package com.xr.hr.care;

import com.xr.common.CommUtil;
import com.bstek.dorado.common.*;
import com.bstek.dorado.data.*;
import com.bstek.dorado.data.db.DBStatement;
import com.bstek.dorado.view.*;
import com.bstek.dorado.view.data.*;
import com.bstek.dorado.view.control.*;

/**
 * applyNewViewModel
 */
public class applyNewViewModel extends DefaultViewModel {
  protected void initDatasets()
      throws Exception {
	  
	 // this.getDataset("").setAutoLoadData(false);
	  
    super.initDatasets(); 
  }
  protected void initControls()
      throws Exception {
    super.initControls();

	DoradoContext context = DoradoContext.getContext();
	String action = context.getParameter("action");
	if(action!=null && action.equals("view")){
		getControl("buttonSubmit").setIgnored(true);
		getControl("datapilotCare").setIgnored(true);
	}
	
	
  }

  protected void doUpdateData(ParameterSet parameters, ParameterSet outParameters)
      throws Exception {
    // Add your code here
    super.doUpdateData(parameters, outParameters);
  }
  public void save(ParameterSet parameters, ParameterSet outParameters)
  throws Exception {
	  DBStatement dbs = new DBStatement();
	  Dataset dsCare = getDataset("datasetCareApply");
	  Dataset dsCareApply = getDataset("datasetCareApplySum");
	  
	  CommUtil.dbsUpdateData(dbs, dsCareApply, "emp_care_apply", "emp_care_apply_id", "seq_emp_care");
	  String empCareApplyId=dsCareApply.getString("emp_care_apply_id");
	  
		RecordIterator ri = dsCare.recordIterator();
		ri.setVisibility(Dataset.FILTER_CHANGED);
		while (ri.hasNext()) {
		  Record record = ri.nextRecord();
		  CommUtil.executeUpdate(dbs, "update emp_care set EMP_CARE_APPLY_ID="+empCareApplyId+" where emp_care_id="+record.getString("emp_care_id"));
		}
		ri = dsCare.recordIterator();
		ri.setVisibility(Dataset.FILTER_DELETED);
		while (ri.hasNext()) {
		  Record record = ri.nextRecord();
		  CommUtil.executeUpdate(dbs, "update emp_care set EMP_CARE_APPLY_ID=null where emp_care_id="+record.getString("emp_care_id"));
		}
		  dbs.setSql("update EMP_CARE_APPLY set APPLY_ID="+CommUtil.getEmployeeId()+",APPLY_DATE=sysdate,care_fee=(select sum(care_fee) from emp_care e where e.EMP_CARE_APPLY_ID="+empCareApplyId+"), state=0 where EMP_CARE_APPLY_ID="+empCareApplyId+"");
		  dbs.execute();
		  outParameters.setValue("empCareApplyId",empCareApplyId );
		dbs.close();
}
   
  public void applyZhijie(ParameterSet parameters, ParameterSet outParameters)
  throws Exception {
	  DBStatement dbs = new DBStatement();
	  save(parameters,outParameters);
	  dbs.setSql("update EMP_CARE_APPLY set state=2 where EMP_CARE_APPLY_ID="+outParameters.getString("empCareApplyId")+"");
	  dbs.execute();
	  dbs.setSql("update emp_care set state=2 where  EMP_CARE_APPLY_ID="+outParameters.getString("empCareApplyId"));
	  dbs.execute();
	  
	  dbs.close();
  }
  public void apply(ParameterSet parameters, ParameterSet outParameters)
  throws Exception {
	  
  }
}
